﻿using Microsoft.IdentityModel.Protocols;
using MongoDB.Bson;
using TouchSocket.Core;

namespace Dark_Core_Server.Models.Article
{
    /// <summary>
    /// 文章助手
    /// </summary>
    public static class ArticleHelper
    {
        public static ArticleDocument ToDocument(this SimpleArticleRecord articleRecord,ObjectId userId)
        {

            var fileIds = new List<ObjectId>();
            if (articleRecord.FileIds != null && articleRecord.FileIds.Length > 0)
            {
                articleRecord.FileIds.ForEach(x =>
                {
                    fileIds.Add(ObjectId.Parse(x));
                });
            }

            var result = new ArticleDocument()
            {
                UserId = userId,
                Title = articleRecord.Title,
                Content = articleRecord.Content,
                Synopsis = articleRecord.Synopsis,
                ViewLevel = articleRecord.ViewLevel ?? 0,
                Tags = articleRecord.Tags?? Array.Empty<string>(),
                FileIds = fileIds.ToArray()
            };
            return result;
        }

        /// <summary>
        /// 转换至文章查询模式
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static ArticleQueryMode ToArticleQueryMode(this string mode)
        {
            var result = mode.ToUpper() switch
            {
                "ALL" => ArticleQueryMode.All,
                "TAG"=> ArticleQueryMode.Tag,
                "EXACTTITLE"=>ArticleQueryMode.ExactTitle,
                "FUZZYTITLE"=>ArticleQueryMode.FuzzyTitle,
                "AUTHORID" => ArticleQueryMode.AuthorID,
                "ARTICLEID" => ArticleQueryMode.ArticleID,
                "AUTHORIDANDFUZZYTITLE" => ArticleQueryMode.AuthorIdAndFuzzyTitle,
                "AUTHORIDANDTAG"=>ArticleQueryMode.AuthorIdAndTag,
                _ => ArticleQueryMode.All
            };
            return result;
        }

        
    }
}
